#  导入base模块
import time

from selenium.webdriver.common.by import By

from common.base import Base
from page.background.login_page import LoginPage
from common.functions import browser_operation


class ReceivingAddress(Base):

    #  点击收货地址
    def btn_receiving_address(self):
        self.click((By.XPATH, '/html/body/div[6]/div[1]/div/div/div/div/a[4]'))

    #  定位请选择省
    def select_province(self, value):
        self.Select((By.ID, 'selProvinces_0'), value)

    #  定位请选择市
    def select_city(self, value):
        self.Select((By.ID, 'selCities_0'), value)

    #  定位请选择区,
    def select_district(self, value):
        self.Select((By.ID, 'selDistricts_0'), value)

    #  定位收货人姓名
    def consignee_name(self, cons_name):
        self.send_keys((By.CLASS_NAME, 'inputBg'), cons_name)

    #  定位详细地址
    def detailed_addr(self, det_addr):
        self.send_keys((By.NAME, 'address'), det_addr)

    #  定位电话
    def telephone(self, tel):
        self.send_keys((By.NAME, 'tel'), tel)

    #  定位电子邮件地址
    def email_addr(self, email):
        # 清空再输入
        self.clear((By.NAME, 'email'))
        self.send_keys((By.NAME, 'email'), email)

    #  定位：邮政编码
    def postal_code(self, post_num):
        self.send_keys((By.NAME, 'zipcode'), post_num)

    #  定位手机
    def mobile_phone(self, phone_number):
        self.send_keys((By.NAME, 'mobile'), phone_number)

    #  定位新增收货地址
    def add_receiving_address(self):
        self.click((By.NAME, 'submit'))

    #  定位删除
    def del_addr(self):
        self.click((By.XPATH, '/html/body/div[6]/div[2]/div/div/div/form[1]/table/tbody/tr[5]/td[2]/input[2]'))

    #  确认弹框选择
    def choice_alert(self):
        # 获取弹框对象
        alert = self.driver.switch_to.alert
        # 点击：确认
        alert.accept()


if __name__ == '__main__':
    driver = browser_operation()
    login = LoginPage(driver)
    addr = ReceivingAddress(driver)
    login.get_website('http://139.129.26.163/user.php')
    login.input_username('testlm')
    login.input_password('123456')
    login.btn_login()

    # 调用：新增收货地址流程
    addr.implicitly_wait(5)

    addr.btn_receiving_address()
    addr.select_province('18')
    addr.select_city('204')
    addr.select_district('2145')
    addr.consignee_name('testlm')
    addr.detailed_addr('湖北省武汉市江夏区testlm')
    addr.telephone('027-28865253')
    addr.email_addr('646785213@qq.com')
    addr.postal_code('410009')
    addr.mobile_phone('12412345678')
    # 增加地址
    addr.add_receiving_address()
    time.sleep(2)

    # 删除地址
    addr.del_addr()
    addr.choice_alert()
    time.sleep(2)
    addr.quit(2)
